package com.frxuexi.park2304.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.UserBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserIntent,UserViewModel>() {
    override val binding: ActivityLoginBinding by lazy { ActivityLoginBinding.inflate(layoutInflater) }
    override val vm: UserViewModel by viewModels<UserViewModel>()

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("登录成功")
                        val bean = it.data as UserBean
                        SPUtils.getInstance().apply {
                            put(Const.TOKEN,bean.token)
                            put(Const.UID,bean.id)
                            put(Const.UNAME,bean.username)
                            put(Const.ROLE,bean.roleid)
                        }
                       /*
                       //room登录响应
                       val num = it.data as Int
                        if(num>0){
                            ToastUtils.showLong("登录成功")
                        }else{
                            ToastUtils.showLong("登录失败")
                        }*/
                        start()
                    }

                    else -> {}
                }
            }
        }
    }

    fun start(){
        val role = SPUtils.getInstance().getInt(Const.ROLE, -1)
        if(role!=-1){
            if(SPUtils.getInstance().getInt(Const.ROLE, -1)==19) {
                ArouterManager.nav(this,"/app/MainActivity")
            }else{
                ArouterManager.nav(this,"/app/MainActivity")
//                ArouterManager.nav(this,"/app/WebViewActivity")
            }
            finish()
        }
    }

    override fun initView() {
        start()

        binding.btnLogin.setOnClickListener {
            val uname = binding.loginUname.text.toString()
            val upass = binding.loginPass.text.toString()

            if(uname.isNullOrEmpty() || upass.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }

//            1[0-9]{10}
            if(!uname.matches(Regex("[A-z0-9_]{2,8}"))){
                ToastUtils.showLong("不是合法用户名")
                return@setOnClickListener
            }

            vm.sendIntent(UserIntent.login(uname,upass))
        }

        binding.loginRegister.setOnClickListener {
            ArouterManager.nav(this,"/app/RegisterActivity")
        }
    }

}